package com.lijin.study.java.thread.concurr;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorTest {

	public static void main(String[] args) {
		ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
		List<Future<String>> fLst = new ArrayList<Future<String>>();
		for (int i = 0; i < 5; i ++) {
			final int flg = i;
			Future<String> f = executor.submit(new Callable<String>() {
				@Override
				public String call() throws Exception {
					System.out.println(Thread.currentThread().getName() + "-a-" + flg);
					Thread.sleep(5000);
					System.out.println(Thread.currentThread().getName() + "-b-" + flg);
					return "succ";
				}
			});
			fLst.add(f);
		}
		executor.shutdown();
		
		try {
			while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
				System.out.println();
			}
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		System.exit(1);
	}
	
}

class MyThread extends Thread {
	
	public MyThread(String name) {
		super(name);
	}

	@Override
	public void run() {
		System.out.println(this.getName() + " begins Sleep...");
		try {
			Thread.sleep(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(this.getName() + " is awaken...");
	}
	
}
